package com.jlboot.app.common.controller;

import com.jfinal.core.ActionKey;
import com.jfinal.core.Controller;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Record;
import com.jlboot.core.json.JsonResult;
import com.jlboot.core.plugins.UploadUtil;

import java.io.File;

/**
 * @author wangsanfei
 * @version 1.0
 * Create by 2024/4/1 12:33
 */

public class FileController extends Controller {

    @ActionKey("/file")
    public void file(){
        String fileName=getPara("fileName");
        if(StrKit.isBlank(fileName)){
            fileName=getAttrForStr("fileName");
        }
        File file=new File(UploadUtil.FILE_SAVE_PATH+fileName);
        if(!file.exists()){
            renderError(404);
            return;
        }
        renderFile(file);
    }

    @ActionKey("/upload")
    public void upload(){

        UploadUtil uploadUtil=UploadUtil.n();
        uploadUtil.upload(getRequest(),"common",false);

        renderJson(JsonResult.ok(uploadUtil.view_path));

    }

    @ActionKey("/upload/ckeditor")
    public void ckeditor_upload(){

        UploadUtil uploadUtil=UploadUtil.n();
        uploadUtil.upload(getRequest(),"common",false);

        Record r=new Record();
        r.set("uploaded",true);
        //https://ckeditor.com/docs/ckeditor5/latest/features/images/image-upload/simple-upload-adapter.html
        r.set("url",uploadUtil.view_path);
        renderJson(r);

    }



}
